clc; close all; clear;
addpath('../02_Functions');

%------------------------------------------------------------------------%
% This file creates figure 6(b) in the paper 
%------------------------------------------------------------------------%

% QUOTES
file_name = '../01_data/es_quotes_5minute_EST';

% choose sample period
start_date            = datenum(1998,1,5);
finish_date           = datenum(2020,12,29);

% Compute returns
winsorizing           = 0;   
alpha                 = 0.99;       
replace_option        = 1; 

results               = make_returns_288(file_name,start_date,finish_date,winsorizing,alpha,replace_option);
caldt                 = results.caldt;

log_return_AD         = results.log_return_AD;

% Hours;
H1 = 1;
H2 = 2;
H3 = 13;
H4 = 16;
H5 = 24;

log_rets_H1           = -1*sum(log_return_AD(:,12*(H1-1)+1:12*H1),2);
log_rets_H2           = sum(log_return_AD(:,12*(H2-1)+1:12*H2),2);
log_rets_H3           = sum(log_return_AD(:,12*(H3-1)+1:12*H3),2);
log_rets_H4           = -1*sum(log_return_AD(:,12*(H4-1)+1:12*H4),2);
log_rets_H5           = -1*sum(log_return_AD(:,12*(H5-1)+1:12*H5),2);

rets_H1               = exp(log_rets_H1)-1;
rets_H2               = exp(log_rets_H2)-1;
rets_H3               = exp(log_rets_H3)-1;
rets_H4               = exp(log_rets_H4)-1;
rets_H5               = exp(log_rets_H5)-1;

cum_rets_H1           = cumprod(1+rets_H1);
cum_log_rets_H1       = cumsum(log_rets_H1);

cum_rets_H2           = cumprod(1+rets_H2);
cum_log_rets_H2       = cumsum(log_rets_H2);

cum_rets_H3           = cumprod(1+rets_H3);
cum_log_rets_H3       = cumsum(log_rets_H3);

cum_rets_H4           = cumprod(1+rets_H4);
cum_log_rets_H4       = cumsum(log_rets_H4);

cum_rets_H5           = cumprod(1+rets_H5);
cum_log_rets_H5       = cumsum(log_rets_H5);

% number for text 
log_return_H1_end       = cum_log_rets_H1(end);
log_return_H2_end       = cum_log_rets_H2(end);
log_return_H3_end       = cum_log_rets_H3(end);
log_return_H4_end       = cum_log_rets_H4(end);
log_return_H5_end       = cum_log_rets_H5(end);

return_H1_end           = exp(log_return_H1_end);
return_H2_end           = exp(log_return_H2_end);
return_H3_end           = exp(log_return_H3_end);
return_H4_end           = exp(log_return_H4_end);
return_H5_end           = exp(log_return_H5_end);

str_H1                  = ['\leftarrow $',num2str(return_H1_end,'%2.2f')];
str_H2                  = ['\leftarrow $',num2str(return_H2_end,'%2.2f')];
str_H3                  = ['\leftarrow $',num2str(return_H3_end,'%2.2f')];
str_H4                  = ['\leftarrow $',num2str(return_H4_end,'%2.2f')];
str_H5                  = ['\leftarrow $',num2str(return_H5_end,'%2.2f')];


% -cum returns ----------------------------------

LW = 4;
FS = 18;


figure;
hold on
plot(caldt , cum_log_rets_H4,'Linestyle','-','Linewidth',LW,'Color',rgb('purple'));
hold on
plot(caldt , cum_log_rets_H5,'Linestyle','-','Linewidth',LW,'Color',rgb('orange'));
text(finish_date,cum_log_rets_H4(end) , str_H4 ,'FontSize',FS,'color','black')
text(finish_date,cum_log_rets_H5(end) , str_H5 ,'FontSize',FS,'color','black')
hold on
legend('short 09:00-10:00' , 'short 17:00-18:00','Location','NorthWest')
xlabel('Year' )
ylabel('Cumulative Returns to $1 Investment' )
datetick('x',11)
xlim([min(caldt)-66 max(caldt)+66])
